package com.myway.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * @ClassName: PropertiesUtil <br/>
 * @Description: Properties工具类 <br/>
 * @CreateDate: 2016年5月31日 下午4:26:09 <br/>
 * @author Toby
 * @version V1.0
 */
public class PropertiesUtil {

	private static final Logger log = Logger.getLogger(PropertiesUtil.class);

	// 基础配置文件
	private static final String BASIC_CONFIG = "ctx-basic.properties";
	// 返回信息配置
	private static final String MESSAGE_CONFIG = "message.properties";
	//网上值机配置文件
	private static final String CHECKIN_CONFIG ="ctx-checkin.properties";
	//公共配置文件
	private static final String COMPONENTS_CONFIG ="ctx-components.properties";
	//辅营
	private static final String AUXILIARYMARKETING_CONFIG="ctx-auxiliaryMarketing.properties";
	
	private static final String  SQLTEMPLATE_CONFIG="ctx-sqltemplate.properties";
	
	private static final String SMS_CONFIG="ctx-sms.properties";
	
	// 短信模板
	public static Properties smsProperties = new Properties();
	// 基础配置
	public static Properties configProperties = new Properties();
	// 返回信息
	public static Properties msgProperties = new Properties();
	//网上值机
	public static Properties checkinPro = new Properties();
	//公共
	public static Properties componentsPro = new Properties();
	//辅营
	public static Properties auxiliaryMarketingPro = new Properties();
	
	public static Properties sqltemplatePro = new Properties();

	/**
	 * 统一在这里完成初始化
	 */
	static {
		configProperties = loadProperties(BASIC_CONFIG);
		msgProperties = loadProperties(MESSAGE_CONFIG);
		checkinPro=loadProperties(CHECKIN_CONFIG);
		componentsPro=loadProperties(COMPONENTS_CONFIG);
		auxiliaryMarketingPro=loadProperties(AUXILIARYMARKETING_CONFIG);
		sqltemplatePro=loadProperties(SQLTEMPLATE_CONFIG);
		smsProperties=loadProperties(SMS_CONFIG);
	}

	/**
	 * @名称：loadProperties
	 * @描述：加载property文件
	 * @param resources
	 * @return
	 */
	public static Properties loadProperties(String resources) {
		ClassLoader classLoader = PropertiesUtil.class.getClassLoader();
		// 使用InputStream得到一个资源文件
		InputStream inputstream = classLoader.getResourceAsStream(resources);
		// new 一个Properties
		Properties properties = new Properties();
		try {
			// 加载配置文件
			properties.load(inputstream);
			return properties;
		} catch (IOException e) {
			log.error("properties加载失败:" + e);
			throw new RuntimeException(e);
		} finally {
			try {
				inputstream.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

	/**
	 * @Name: getStr <br/>
	 * @description: 获取配置文件信息 <br/>
	 * @param properties
	 * @param key
	 * @return
	 */
	public static String getProStr(Properties properties, String key) {
		String value = null;
		if (null != properties) {
			value = properties.getProperty(key);
		}
		return value;
	}

	/**
	 * @Name: getProInteger <br/>
	 * @description: 获取配置文件信息 <br/>
	 * @param properties
	 * @param key
	 * @return
	 */
	public static Integer getProInteger(Properties properties, String key) {
		Integer value = null;
		try {
			if (null != properties) {
				String v = properties.getProperty(key);
				if (null != v) {
					value = Integer.parseInt(v);
				}
			}
		} catch (Exception e) {
			
		}
		return value;
	}

	/**
	 * @Name: getProBoolean <br/>
	 * @description: 获取配置文件信息 <br/>
	 * @param properties
	 * @param key
	 * @return
	 */
	public static Boolean getProBoolean(Properties properties, String key) {
		Boolean value = null;
		try {
			if (null != properties) {
				String v = properties.getProperty(key);
				if (null != v) {
					value = Boolean.parseBoolean(v);
				}
			}
		} catch (Exception e) {

		}
		return value;
	}

	/**
	 * @Name: getPropertyBykey <br/>
	 * @description: 不建议使用 <br/>
	 * @param resource
	 * @param key
	 * @return
	 */
	public static Object getPropertyBykey(String resource, String key) {
		Properties p = loadProperties(resource);
		return p.get(key);
	}
}
